package com.jade.linkong.controller;

import com.google.common.base.Joiner;
import com.jade.common.annotation.Log;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.core.domain.Ztree;
import com.jade.common.core.page.TableDataInfo;
import com.jade.common.enums.BusinessType;
import com.jade.common.utils.StringUtils;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.LkMatter;
import com.jade.linkong.domain.LkMatterType;
import com.jade.linkong.domain.LkTemplate;
import com.jade.linkong.service.ILkTemplateService;
import com.jade.linkong.service.impl.LkMatterServiceImpl;
import com.jade.linkong.service.impl.LkMatterTypeServiceImpl;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 前期模板Controller
 * 
 * @author jade
 * @date 2020-07-14
 */
@Controller
@RequestMapping("/linkong/template")
public class LkTemplateController extends BaseController
{
    private String prefix = "linkong/template";

    @Autowired
    private ILkTemplateService lkTemplateService;
    @Autowired
    private LkMatterTypeServiceImpl lkMatterTypeServiceImpl;
    @Autowired
    private LkMatterServiceImpl lkMatterServiceImpl;

    @RequiresPermissions("linkong:template:view")
    @GetMapping()
    public String template()
    {
        return prefix + "/template";
    }

    /**
     * 查询前期模板列表
     */
    @RequiresPermissions("linkong:template:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LkTemplate lkTemplate)
    {
        startPage();
        List<LkTemplate> list = lkTemplateService.selectLkTemplateList(lkTemplate);
        return getDataTable(list);
    }

    /**
     * 导出前期模板列表
     */
    @RequiresPermissions("linkong:template:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LkTemplate lkTemplate)
    {
        List<LkTemplate> list = lkTemplateService.selectLkTemplateList(lkTemplate);
        ExcelUtil<LkTemplate> util = new ExcelUtil<LkTemplate>(LkTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 新增前期模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存前期模板
     */
    @RequiresPermissions("linkong:template:add")
    @Log(title = "前期模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LkTemplate lkTemplate)
    {
        lkTemplate.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(lkTemplateService.insertLkTemplate(lkTemplate));
    }

    /**
     * 修改前期模板
     */
    @GetMapping("/edit/{tempId}")
    public String edit(@PathVariable("tempId") String tempId, ModelMap mmap)
    {
        LkTemplate lkTemplate = lkTemplateService.selectLkTemplateById(tempId);
        mmap.put("lkTemplate", lkTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存前期模板
     */
    @RequiresPermissions("linkong:template:edit")
    @Log(title = "前期模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LkTemplate lkTemplate)
    {
        return toAjax(lkTemplateService.updateLkTemplate(lkTemplate));
    }

    /**
     * 删除前期模板
     */
    @RequiresPermissions("linkong:template:remove")
    @Log(title = "前期模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lkTemplateService.deleteLkTemplateByIds(ids));
    }
    
    /**
     * 选择事项分类树
     */
    @GetMapping("/selectMatterTypeTree/{deptId}")
    public String selectMatterTypeTree(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {

        return prefix + "/tree";
    }

    /**
     * 加载事项分类列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(Integer tempId)
    {
        List<Ztree> ztrees = lkMatterTypeServiceImpl.selectMatterTypeTree(tempId);
        return ztrees;
    }
}
